import functools

# 公用的函数定义在这里
from flask import current_app
from flask import g
from flask import session

from info.models import User


def indexClass(index):
    if index == 0:
        return 'first'
    elif index == 1:
        return 'second'
    elif index == 2:
        return 'third'


# 定义装饰器实现查询用户状态的功能
def user_login_data(f):
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id", None)
        # 如果取到用户id,从数据库中查询用户是否存在
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        g.user = user
        return f(*args, **kwargs)

    return wrapper
